<template>
  <div class="app-container">
    <el-row  style="">
      <el-col :span="3">
        <div class="grid-content bg-purple">
          <span class="grid">昵称</span>
          &nbsp; &nbsp;
          <el-input
            style="width: 80%"
            v-model="params.nickname"
            placeholder="请输入内容"
          ></el-input>
        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content bg-purple">
          <span class="grid">状态</span>
          &nbsp; &nbsp;
          <el-select v-model="params.status" placeholder="请选择状态">
            <el-option label="管理员" value="1"></el-option>
            <el-option label="普通用户" value="0"></el-option>
          </el-select>
        </div
        >
      </el-col>
      <el-col :span="6">
        <div class="grid-content bg-purple">
          <span class="grid">时间</span>
          &nbsp; &nbsp;
          <el-date-picker
            v-model="value7"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            @change="change"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions2"
          >
          </el-date-picker>
        </div
        >
      </el-col>
      <el-col :span="1">
        <el-button @click="fetchData" type="primary" icon="el-icon-search">查询</el-button>
      </el-col>
      <el-col :span="1" style="margin-left: 30px">
        <el-button style="color: #FF7968"  @click="clear"  icon="el-icon-refresh" plain>重置</el-button>
      </el-col>
      <el-col :span="1" style="margin-left: 30px">

        <el-button type="primary"  @click="clear"  icon="el-icon-plus" plain>新增</el-button>
      </el-col>
    </el-row>
    <el-row>

    </el-row>
    <br/>
    <el-table
      v-loading="listLoading"
      :data="list"
      show-header
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="userId" width="95">
        <template slot-scope="scope">
          <!-- {{ scope.$index }} -->
          {{ scope.row.userId }}
        </template>
      </el-table-column>
      <el-table-column label="昵称">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column label="头像" width="110" align="center">
        <template slot-scope="scope">
          <img
            style="width: 40px; height: 40px; border-radius: 3px"
            :src="scope.row.avatar"
            alt=""
          />

          <!-- <span>{{ scope.row.author }}</span> -->
        </template>
      </el-table-column>
      <el-table-column label="身份" width="110" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1" type="success">管理员</el-tag>
          <el-tag v-if="scope.row.type == 0" type="success">普通用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        class-name="status-col"
        label="Status"
        width="110"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.status == 1">被锁定</el-tag>
          <el-tag v-if="scope.row.status == 0" type="">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="created_at"
        label="注册时间"
        width="200"
      >
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.gmtCreate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <i style="color: #409eff" class="el-icon-edit"></i>
          <el-button @click="edit(scope.row.userId)" type="text"
          >修改
          </el-button
          >
          <i style="color: #409eff" class="el-icon-delete"></i>
          <el-button
            @click="open2(scope.row.userId)"
            slot="reference"
            type="text"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-sizes="[6, 10, 30, 50]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {getList} from "@/api/table";
  import {listUsers} from "@/api/user";
  import {deleteUserById} from "@/api/user";

  export default {
    name: 'userlist',
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: "success",
          draft: "gray",
          deleted: "danger",
        };
        return statusMap[status];
      },
    },
    data() {
      return {
        list: null,
        total: 0,
        listLoading: true,
        params: {
          pageNum: 1,
          limit: 6,
          nickname: "",
          date: [],
          status: '',
        },
        pickerOptions2: {
          shortcuts: [
            {
              text: "最近一周",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit("pick", [start, end]);
              },
            },
            {
              text: "最近一个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit("pick", [start, end]);
              },
            },
            {
              text: "最近三个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit("pick", [start, end]);
              },
            },
          ],
        },
        value6: "",
        value7: "",
      };
    },
    created() {
      this.fetchData();
    },
    methods: {
      clear() {
        this.params = {};
        this.params.pageNum = 1;
        this.params.limit = 6;
        this.params.nickname = "";
        this.params.date = [];
        this.params.status = 0;
        this.fetchData();
        this.params.status = ''; // 显示好看

      },
      change(val) {
        this.params.date = this.value7;
      },
      handleSizeChange(val) {
        console.log(val);
        this.params.limit = val;
        this.fetchData();
      },
      handleCurrentChange(val) {
        this.params.pageNum = val;
        this.fetchData();
      },
      fetchData() {
        this.listLoading = true;
        listUsers(this.params).then((res) => {
          console.log(res);
          this.listLoading = false;
          this.total = res.data.total;
          this.list = res.data.userList;
          console.log(this.list);
        });
        // getList(this.listQuery).then(response => {
        // this.list = response.data.items
        // this.listLoading = false;
        // })
      },
      open2(userId) {
        this.$confirm("是否删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deleteUserById(userId)
              .then((res) => {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.fetchData();
              })
              .catch((reason) => {
                this.$message({
                  type: "error",
                  message: resason,
                });
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      edit(userId) {
        this.$router.push({
          path: "/user/edit",
          query: {
            userId: userId,
          },
        });
      },
    },
  };
</script>

<style scoped>
  .grid-content {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .grid {
    font-weight: 600;
    color: #606266;
  }

  /deep/ .has-gutter th {
    background-color: #F8F8F9;
  }
</style>>

